package view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;

import model.Cell;
import model.CommentCell;
import model.Sheet;
import xl.Address;

public class SlotView extends JLabel {
	private Address address;
	private CurrentView currentView;
	private SlotView[][] slotView;
	private Editor editor;
	private Sheet sheet;

	public SlotView(CurrentView currentView, Address address,
			SlotView[][] slotView, Editor editor, Sheet sheet) {
		super("                    ", RIGHT);
		setBackground(Color.WHITE);
		setOpaque(true);
		addMouseListener(new ClickListener());
		this.address = address;
		this.currentView = currentView;
		this.slotView = slotView;
		this.editor = editor;
		this.sheet = sheet;
	}

	private class ClickListener extends MouseAdapter {
		public void mouseClicked(MouseEvent event) {
			Address ad = new Address(currentView.getText());
			slotView[ad.row()][ad.col()].setBackground(Color.WHITE);
			currentView.update(address);
			sheet.update();
			setBackground(Color.PINK);
			ad = new Address(currentView.getText());
			Cell cell = sheet.getSheet().get(ad);
			if (cell instanceof CommentCell) {
				editor.setText("#" + slotView[ad.row()][ad.col()].getText());
			} else {
				if(cell != null){
					editor.setText(cell.toString());
				}else{
					editor.setText(slotView[ad.row()][ad.col()].getText());
				}
				
			}
			
		}
	}
}